﻿package 
{
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.KeyboardEvent;
	import flash.net.*;
	import flash.system.*;
	import flash.text.TextField;

	/*******************
	*
	*@Author Luanluan
	*
	******俄罗斯方块
	*******************/

	public class Main extends Sprite
	{
		//世界
		public var world:World = new World(Config._ROW,Config._COL);


		public var worldArr:Array = new Array  ;
		private var currentArr:Array = new Array  ;
		private var currentSq:Array = new Array  ;
		private var floorSq:Array = new Array  ;
		private var worldContainer:Sprite = new Sprite  ;

		private var nextWorld:World = new World(6,6);
		public var nextWorldArr:Array = new Array  ;
		private var nextArr:Array = new Array  ;
		private var nextSq:Array = new Array  ;
		private var nextContainer:Sprite = new Sprite  ;

		private var currentX:int = 4;
		private var currentY:int = 0;
		private var currentRotation:int = 0;

		//计分面板
		private var scoreBoard:ScoreBoard = new ScoreBoard  ;

		private var score:uint = 0;
		private var level:uint = 1;

		private var speed:Number = 1;//初始速度

		private var myTimer:Timer;

		public function Main()
		{
			//初始化
			init();
		}
		public function init():void
		{
			timerSet();
			//视图初始化;

			world.x = Config.X0;
			world.y = Config.Y0;
			addChild(world);
			worldArr = world.WorldArr();
			nextWorldArr = nextWorld.WorldArr();

			scoreBoard.x = world.x + world.width + 40;
			scoreBoard.y = world.y;
			scoreBoard.ScoreText.text = "Score : " + score.toString();
			scoreBoard.ScoreText.setTextFormat(scoreBoard.format);
			scoreBoard.LevelText.text = "Level : " + level.toString();
			scoreBoard.LevelText.setTextFormat(scoreBoard.format);
			addChild(scoreBoard);
			nextWorld.x = scoreBoard.x;
			nextWorld.y = scoreBoard.y + 25;
			addChild(nextWorld);

			nextArr = Config.typeL;
			createGrille();
			initWorld();
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeydown);
		}
		public function initWorld():void
		{
			//trace(worldArr.join("\n")+"\n"+"======================");
			//trace("this.numChildren="+worldContainer.numChildren);
			//trace(currentSq)
			clearWorld();//清屏
			currentSq = [];
			for (var h:int = 0; h < Config._ROW; h++)
			{
				for (var k:int = 0; k < Config._COL; k++)
				{
					switch (worldArr[h][k])
					{
						case 1 ://当前方块
							var sq:Square = new Square(Config._SIZE,Config._SIZE,0xff66ff,h,k);
							sq.x = k * Config._SIZE + Config.X0;
							sq.y = h * Config._SIZE + Config.Y0;
							currentSq.push(sq);
							worldContainer.addChild(sq);
							break;
						case 2 ://落地方块
							var sq2:Square = new Square(Config._SIZE,Config._SIZE,0x0099ff,h,k);
							sq2.x = k * Config._SIZE + Config.X0;
							sq2.y = h * Config._SIZE + Config.Y0;
							floorSq.push(sq2);
							worldContainer.addChild(sq2);
							break;
					}
				}
			}
			addChild(worldContainer);

			for (var m:int = 0; m < 6; m++)
			{
				for (var n:int = 0; n < 6; n++)
				{
					switch (nextWorldArr[m][n])
					{
						case 1 :
							var sq1:Square = new Square(Config._SIZE,Config._SIZE,0xccff99,m,n);
							sq1.x = n * Config._SIZE + scoreBoard.x;
							sq1.y = (m) * Config._SIZE + scoreBoard.y+25;
							nextSq.push(sq1);
							nextContainer.addChild(sq1);
							break;

					}
				}
			}
			addChild(nextContainer);
		}

		public function clearWorld():void
		{
			//清屏
			if (currentSq.length != 0)
			{

				for (var i:int = 0; i < 4; i++)
				{
					worldContainer.removeChild(currentSq[i]);

				}
				removeChild(worldContainer);
				worldContainer = new Sprite  ;
				//for (var j:int=10; j<this.numChildren; j++)
				//{
				//this.getChildIndex(j);
				//}
				currentSq = [];
			}

			if (nextSq.length != 0)
			{


				for (var j:int = 0; j < 4; j++)
				{
					nextContainer.removeChild(nextSq[j]);
				}
				removeChild(nextContainer);
				nextContainer = new Sprite  ;
				nextSq = [];
			}


		}
		public function timerSet():void
		{
			if (myTimer != null)
			{
				myTimer.removeEventListener(TimerEvent.TIMER,timerhandler);
			}
			myTimer=new Timer(1000 / ( speed * level ) ) ;
			myTimer.addEventListener(TimerEvent.TIMER,timerhandler);
			myTimer.start();
		}
		public function createGrille():void
		{
			//创建方块
			currentRotation = Math.random() * 4;
			currentArr = nextArr;
			for (var j:int = 0; j < 4; j++)
			{
				var arr:Array = currentArr[currentRotation * 4 + j];
				worldArr[arr[1]][arr[0] + 4] = 1;
			}

			var i:int = int(Math.random() * 7);
			switch (i)
			{
				case 0 :
					nextArr = Config.typeL;
					break;
				case 1 :
					nextArr = Config.typeB;
					break;
				case 2 :
					nextArr = Config.typeZ;
					break;
				case 3 :
					nextArr = Config.typeS;
					break;
				case 4 :
					nextArr = Config.typeI;
					break;
				case 5 :
					nextArr = Config.typeT;
					break;
				case 6 :
					nextArr = Config.typeJ;
					break;
			}
			for (var m:int = 0; m <6; m++)
			{
				for (var n:int = 0; n <6; n++)
				{
					if (nextWorldArr[m][n] == 1)
					{
						nextWorldArr[m][n] = 0;
					}
				}
			}
			for (var h:int = 0; h < 4; h++)
			{
				var arr1:Array = nextArr[h];
				nextWorldArr[arr1[1] + 1][arr1[0] + 1] = 1;
			}


		}

		public function onKeydown(e:KeyboardEvent):void
		{
			switch (e.keyCode)
			{
				case 38 :
					_Roll();
					break;
				case 37 :
					_Left();
					break;
				case 39 :
					_Right();
					break;
				case 40 :
					_Down();
					break;
				default :
					break;

			}

		}


		public function timerhandler(e:TimerEvent):void
		{
			_Down();
		}


		//下移
		public function _Down():void
		{
			if (currentSq.length == 0)
			{
				return;
			}

			for (var i:int = 0; i < 4; i++)
			{
				//超出则落地
				if (currentSq[i].px >= Config._ROW - 2 || worldArr[currentSq[i].px + 1][currentSq[i].py] == 2)
				{
					_Floor(currentSq);
					return;
				}
			}


			for (var h:int = 0; h < Config._ROW; h++)
			{
				for (var k:int = 0; k < Config._COL; k++)
				{
					if (worldArr[h][k] == 1)
					{
						worldArr[h][k] = 0;
					}
				}
			}

			for (var j:int = 0; j < 4; j++)
			{
				currentSq[j].px++;
				//currentSq[i].py++;
				worldArr[currentSq[j].px][currentSq[j].py] = 1;
			}
			initWorld();
			currentY++;
		}
		public function _Left():void
		{

			if (currentSq.length == 0)
			{
				return;
			}

			for (var i:int = 0; i < 4; i++)
			{

				//是否超出边界
				if (currentSq[i].py <= 0 || worldArr[currentSq[i].px][currentSq[i].py - 1] == 2)
				{
					return;
				}
			}
			for (var h:int = 0; h < Config._ROW; h++)
			{
				for (var k:int = 0; k < Config._COL; k++)
				{
					switch (worldArr[h][k])
					{
						case 1 :
							worldArr[h][k] = 0;
							break;
					}
				}
			}

			for (var j:int = 0; j < 4; j++)
			{
				currentSq[j].py--;
				worldArr[currentSq[j].px][currentSq[j].py] = 1;
			}
			initWorld();
			currentX--;
		}


		public function _Right():void
		{

			if (currentSq.length == 0)
			{
				return;
			}

			for (var i:int = 0; i < 4; i++)
			{

				//是否超出边界
				if (currentSq[i].py >= Config._COL - 2 || worldArr[currentSq[i].px][currentSq[i].py + 1] == 2)
				{
					return;
				}
			}
			for (var h:int = 0; h < Config._ROW; h++)
			{
				for (var k:int = 0; k < Config._COL; k++)
				{
					switch (worldArr[h][k])
					{
						case 1 :
							worldArr[h][k] = 0;
							break;
					}
				}
			}
			for (var j:int = 0; j < 4; j++)
			{
				currentSq[j].py++;
				worldArr[currentSq[j].px][currentSq[j].py] = 1;
			}

			initWorld();
			currentX++;
		}


		public function _Roll():void
		{
			//翻转
			currentRotation++;

			if (currentRotation >= 4)
			{
				currentRotation = 0;
			}

			for (var h:int = 0; h < Config._ROW; h++)
			{
				for (var k:int = 0; k < Config._COL; k++)
				{
					switch (worldArr[h][k])
					{
						case 1 :
							worldArr[h][k] = 0;
							break;
					}
				}
			}

			for (var i:int = 0; i < 4; i++)
			{
				var arr:Array = currentArr[currentRotation * 4 + i];
				if (worldArr[currentY + arr[1]][arr[0] + currentX] == 2 || arr[0] + currentX < 0 || arr[0] + currentX >= Config._COL - 1)
				{
					trace("return");
					return;
				}

				worldArr[currentY + arr[1]][arr[0] + currentX] = 1;
			}
			initWorld();
		}
		//落地
		public function _Floor(arr:Array):void
		{
			for (var i:int = 0; i < currentSq.length; i++)
			{
				var sq:Square = currentSq[i];
				floorSq.push(sq);
				worldArr[arr[i].px][arr[i].py] = 2;
			}
			currentSq = [];
			currentArr = [];
			currentX = 4;
			currentY = 0;
			createGrille();
			initWorld();
			checkisFull();
		}
		public function checkisFull():void
		{
			var floorRow:uint = 0;
			for (var i:int = Config._ROW - 1; i >= 0; i--)
			{

				var floorNum:uint = 0;
				for (var j:int = Config._COL - 1; j >= 0; j--)
				{
					if (worldArr[i][j] == 2)
					{
						floorNum++;
					}
				}

				if (floorNum >= Config._COL - 1)
				{
					floorRow++;
					for (var h:int = Config._COL - 1; h >= 0; h--)
					{
						worldArr[i][h] = 0;
					}
					for (var m:int = i; m > 0; m--)
					{
						for (var n:int = Config._COL - 1; n >= 0; n--)
						{
							if (worldArr[m - 1][n] == 2)
							{
								worldArr[m][n] = 2;
								worldArr[m - 1][n] = 0;
							}
						}
					}
					i++;

				}
				initWorld();
			}
			score +=  int(Config._SCORE[floorRow - 1]);
			scoreBoard.ScoreText.text = "Score : " + score.toString();
			scoreBoard.ScoreText.setTextFormat(scoreBoard.format);
			if (score >= level * 200)
			{
				level++;
				timerSet();
				scoreBoard.LevelText.text = "Level : " + level.toString();
				scoreBoard.LevelText.setTextFormat(scoreBoard.format);
			}
			if (level >= 10)
			{
				gameOver();
			}
			for (var p:int=Config._ROW; p>=0; p--)
			{
				if (worldArr[0][p] == 2)
				{
					gameOver();
				}
			}
		}
		public function gameOver():void
		{
			var overText:TextField=new  TextField();
			overText.text = "Game Over!";
			overText.x = scoreBoard.x;
			overText.y = scoreBoard.y - 15;
			overText.setTextFormat(scoreBoard.format);
			addChild(overText);
			myTimer.removeEventListener(TimerEvent.TIMER,timerhandler);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeydown);
		}

	}

}